package org.theonept.travian.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import android.util.Log;

public class HttpUtil {
	private static HttpClient httpclient;

	public static String excutePost(String urlString, List<NameValuePair> param) {

		try {
			if (httpclient == null) {
				httpclient = new DefaultHttpClient();
				// Create a local instance of cookie store
				CookieStore cookieStore = new BasicCookieStore();

				// Create local HTTP context
				HttpContext localContext = new BasicHttpContext();
				// Bind custom cookie store to the local context
				localContext.setAttribute(ClientContext.COOKIE_STORE,
						cookieStore);
				HttpConnectionParams.setConnectionTimeout(
						httpclient.getParams(), 100000);

			}

			HttpPost httppost = new HttpPost(urlString);
			Log.w("Http Post", "Un post a eu lieu vers " + urlString);
			// Add your data (Var name, Var value)
			httppost.getParams().setParameter(
					CoreProtocolPNames.HTTP_CONTENT_CHARSET, "UTF-8");

			httppost.getParams().setParameter(
					CoreProtocolPNames.HTTP_ELEMENT_CHARSET, "UTF-8");

			httppost.getParams().setParameter(
					CoreProtocolPNames.USE_EXPECT_CONTINUE, Boolean.FALSE);

			httppost.setEntity(new UrlEncodedFormEntity(param, "utf-8"));

			// Execute HTTP Post Request
			HttpResponse response = httpclient.execute(httppost);

			// response.getParams().setParameter(
			// CoreProtocolPNames.HTTP_CONTENT_CHARSET, "UTF-8");
			//
			// response.getParams().setParameter(
			// CoreProtocolPNames.HTTP_ELEMENT_CHARSET, "UTF-8");
			// Saves response
			StringBuilder result = getString(response);

			return result.toString();

		} catch (Exception e) {

			throw new RuntimeException(e);

		}
	}

	private static StringBuilder getString(HttpResponse response)
			throws IOException {
		String line = "";
		StringBuilder result = new StringBuilder();

		BufferedReader rd = new BufferedReader(new InputStreamReader(response
				.getEntity().getContent()));

		while ((line = rd.readLine()) != null) {
			result.append(line);
			result.append("\n");
		}
		rd.close();
		return result;
	}

	public static Document getDocumentFromUrl(String urlString) {
		try {
			if (httpclient == null) {
				httpclient = new DefaultHttpClient();
				// Create a local instance of cookie store
				CookieStore cookieStore = new BasicCookieStore();

				// Create local HTTP context
				HttpContext localContext = new BasicHttpContext();
				// Bind custom cookie store to the local context
				localContext.setAttribute(ClientContext.COOKIE_STORE,
						cookieStore);
				HttpConnectionParams.setConnectionTimeout(
						httpclient.getParams(), 100000);

			}
			HttpGet httpget = new HttpGet(urlString);
			HttpResponse response = httpclient.execute(httpget);
			Log.w("Http Get", "Un get a eu lieu vers " + urlString);
			Document document = Jsoup.parse(getString(response).toString());
			return document;

		} catch (Exception e) {
			e.printStackTrace();

		}
		return null;

	}

}